﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;

namespace UniversityManagementSystem.UI
{
    public partial class ScheduleClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Department department = new Department();
                    Semester semester = new Semester();
                    Building building = new Building();
                    List<Department> allDepartments = new List<Department>();
                    List<Semester> allSemesters = new List<Semester>();
                    List<Building> allBuildings = new List<Building>(); 
                    allSemesters = semester.GetAllSemesters();
                    allDepartments = department.GetAllDepartments();
                    allBuildings = building.GetAllBuildings();
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataTextField = "DepartmentName";
                    departmentDropDownList.DataValueField = "DepartmentCode";
                    departmentDropDownList.DataBind();
                    semesterDropDownList.DataSource = allSemesters;
                    semesterDropDownList.DataTextField = "SemesterName";
                    semesterDropDownList.DataValueField = "SemesterId";
                    semesterDropDownList.DataBind();
                    buildingDropDownList.DataSource = allBuildings;
                    buildingDropDownList.DataValueField = "BuildingId";
                    buildingDropDownList.DataTextField = "BuildingName";
                    buildingDropDownList.DataBind();
                    departmentDropDownList_SelectedIndexChanged(null,null);
                    semesterDropDownList_SelectedIndexChanged(null,null);
                    buildingDropDownList_SelectedIndexChanged(null,null);
                }
            }
            catch (Exception)
            {
                messageLabel.Text = "Error occured while loading the page.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            try
            {
                Course course = new Course();
                List<Course> allCourse = new List<Course>();
                allCourse = course.GelAllCourseBySemester(departmentDropDownList.SelectedValue,
                                                          semesterDropDownList.SelectedValue);
                courseDropDownList.DataSource = allCourse;
                courseDropDownList.DataValueField = "CourseTitle";
                courseDropDownList.DataTextField = "CourseName";
                courseDropDownList.DataBind();
                ViewState["Course"] = allCourse;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load course.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void semesterDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Course course = new Course();
                List<Course> allCourse = new List<Course>();
                allCourse = course.GelAllCourseBySemester(departmentDropDownList.SelectedValue,
                                                          semesterDropDownList.SelectedValue);
                courseDropDownList.DataSource = allCourse;
                courseDropDownList.DataValueField = "CourseTitle";
                courseDropDownList.DataTextField = "CourseName";
                courseDropDownList.DataBind();
                ViewState["Course"] = allCourse;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load course.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void buildingDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Building building = new Building();
                List<Building> allRooms = new List<Building>();
                allRooms = building.GetAllRooms(buildingDropDownList.SelectedValue);
                roomDropDownList.DataSource = allRooms;
                roomDropDownList.DataValueField = "RoomNo";
                roomDropDownList.DataTextField = "RoomNo";
                roomDropDownList.DataBind();
            }
            catch (Exception exception)
            {

                messageLabel.Text = "Unable to load room";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {

            Course course = new Course();
            List<Course> allCourse = new List<Course>();
            ClassSchedule aClassSchedule = new ClassSchedule();
            try
            {
                allCourse = (List<Course>)ViewState["Course"];
                Course findCourse = allCourse.Find(delegate(Course aCourse) { return aCourse.CourseTitle == courseDropDownList.SelectedValue; });
                if (findCourse != null)
                {
                    aClassSchedule.CourseName = findCourse.CourseTitle;
                    aClassSchedule.CourseTeacher = findCourse.CourseTeacher;
                    ViewState["selectedCourse"] = findCourse;
                }
                aClassSchedule.BuildingName = buildingDropDownList.SelectedValue;
                aClassSchedule.RoomNo = roomDropDownList.SelectedValue;
                aClassSchedule.ClassDay = dayDropDownList.SelectedValue;
                string startTime = timeFromTextBox.Text;
                string endTime = timeToTextBox.Text;
                aClassSchedule.StartAmOrPm = startTimeSpanDropDownList.SelectedValue;
                aClassSchedule.EndAmOrPm = endTimeSpanDropDownList.SelectedValue;
                
                string saveScheduleMessage = "";
                saveScheduleMessage = aClassSchedule.SaveSchedule(aClassSchedule,startTime,endTime);
                messageLabel.Text = saveScheduleMessage;
                messageLabel.ForeColor = System.Drawing.Color.Blue;
            }
            
            catch(Exception exception)
            {
                messageLabel.Text = "Unable to save schedule";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}